//
//  CalculatorViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/26.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class CalculatorViewController: BaseViewController {

    var tv0: UITextField!
    var tv1: UITextField!
    
    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tv0 = UITextField(frame: CGRect(x: 0, y: 80, width: self.view.width, height: 30))
        tv0.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
    
        tv1 = UITextField(frame: CGRect(x: 0, y: 120, width: self.view.width, height: 30))
        tv1.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
        
        let resultLabel = UILabel(frame: CGRect(x: 0, y: 160, width: self.view.width, height: 30))
        
        self.view.addSubview(tv0)
        self.view.addSubview(tv1)
        self.view.addSubview(resultLabel)
        
        
        Observable.combineLatest(tv0.rx.text.orEmpty, tv1.rx.text.orEmpty){
            (num0, num1) -> Int in
            
            if !num0.isEmpty && !num1.isEmpty {
                return num0.intValue + num1.intValue
            }
            return 0
            
        }.map({ (value) -> String in
            return "\(value)"
        })
            .bind(to: resultLabel.rx.text.asObserver())
        .disposed(by: bag)
        
        
        
        
        
        
    }

}
